package com.frogs.ucenter.component;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;

import javax.mail.internet.MimeUtility;

import org.apache.velocity.exception.VelocityException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;

import com.frogs.framework.message.email.VelocityMailSupport;

public class VelocityMailSupportHelper extends VelocityMailSupport {
	private static final Logger log = LoggerFactory.getLogger(VelocityMailSupportHelper.class);

	private String to;
	private String cc;
	private String bcc;

	public void sendMime(String nameOfTo, String emailOfTo, String cc, String bcc,
			Map<String, Object> model) throws MailException {
		try {
			nameOfTo = MimeUtility.encodeText(nameOfTo, getDefaultEncoding(), "B");
		} catch (UnsupportedEncodingException e) {
			log.error("func[sendMime] nameOfTo[{}] exception[{} - {}] desc[fail]",
    				new Object[] { nameOfTo, e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		String toAddr = nameOfTo + "<" + emailOfTo + ">";
		sendMime(toAddr, cc, bcc, model);
	}

    public void sendMime(String to, String cc,String bcc,Map<String, Object> model) throws MailException {
        if(cc != null && cc.length() > 0 && bcc != null && bcc.length() > 0){
            String[] ccArray = new String[]{cc};
            String[] bccArray = new String[]{bcc};
            sendMime(to, ccArray,bccArray,model);
        }else{
            sendMime(mergeSimpleMessage(to, model));
        }
    }

    public void sendMime(String nameOfTo,String emailOfTo, String[] cc,String[] bcc,Map<String, Object> model) throws MailException{
        try{
            nameOfTo = MimeUtility.encodeText(nameOfTo,getDefaultEncoding(),"B");
        }catch (UnsupportedEncodingException e){
        	log.error("func[sendMime] nameOfTo[{}] exception[{} - {}] desc[fail]",
    				new Object[] { nameOfTo, e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
        }
        String toAddr = nameOfTo + "<" + emailOfTo + ">";
        sendMime(toAddr,cc,bcc,model);
    }

    public void sendMime(String to, String[] cc,String[] bcc,Map<String, Object> model) throws MailException{
        sendMime(mergeSimpleMessage(to, cc,bcc,model));
    }
    public void sendMime(String to, String[] cc,String[] bcc,Map<String, Object> model,String imgPath) throws MailException{
    	sendMimeContentImg(mergeSimpleMessage(to, cc,bcc,model),imgPath);
    }
    public void sendMime(String to, String[] cc,String[] bcc,Map<String, Object> model,String imgPath,String attachment) throws MailException{
    	sendMimeContentAttachment(mergeSimpleMessage(to, cc,bcc,model),imgPath,attachment);
    }


    /**
     * 重载framework里的mergeSimpleMessage方法，添加抄送地址、密送地址.
     * @param to 收件人地址.
     * @param cc 抄送人地址数组.
     * @param bcc  密送人地址数组.
     * @param model 正文数据map.
     * @return
     */
    public SimpleMailMessage mergeSimpleMessage(String to, String[] cc,String[] bcc,Map<String, Object> model) {
        //render text of mail from velocity template with the data
        String text = null;
        try {
            text = renderText(model);
        } catch (VelocityException e) {
        	log.error("func[mergeSimpleMessage] exception[{} - {}]",
					new Object[] { e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
        }
        //mail message setting
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(getSubject());
        message.setFrom(getFrom());
        message.setTo(to);
        if(cc != null && cc.length > 0){
            message.setCc(cc);
        }
        if(bcc != null && bcc.length > 0){
            message.setBcc(bcc);
        }
        message.setText(text);
        return message;
    }

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getCc() {
		return cc;
	}

	public void setCc(String cc) {
		this.cc = cc;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}
}
